﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoplanetaTaskManager.Models;

namespace AutoplanetaTaskManager.Daos.EntityFramework
{
    public class RoleDao : IRoleDao
    {
        public List<Role> Load()
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                return db.Role.ToList();
            }
        }

        public Role Get(int id)
        {
            using (AtmDbEntities db = new AtmDbEntities())
                return db.Role.FirstOrDefault(m => m.Id == id);
        }

        public Role Get(string name)
        {
            using (AtmDbEntities db = new AtmDbEntities())
                return db.Role.FirstOrDefault(m => m.Name == name);
        }

        public void Add(Role role)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                db.AddToRole(role);
                db.SaveChanges();
            }
        }

        public void Update(Role role)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                // TODO: throw new NotImplementedException();
                db.SaveChanges();
            }
        }

        public void Remove(int id)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                Role role = db.Role.FirstOrDefault(m => m.Id == id);
                if (role != null) db.DeleteObject(role);
                db.SaveChanges();
            }
        }
    }
}
